//
//  OrderViewController.m
//  Courses
//
//  Created by YiXuePxh on 15/4/7.
//  Copyright (c) 2015年 boyibang. All rights reserved.
//

#import "OrderViewController.h"
#import "OrderModel.h"
#import "MyOrderCell.h"


#import "AliOrder.h"
#import "DataSigner.h"
#import <AlipaySDK/AlipaySDK.h>
#import "Product.h"
#import "APAuthV2Info.h"
#import "SubmitOrderVc.h"

#import "ShowOrderVC.h"

@interface OrderViewController ()

@end

@implementation OrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的订单";
    //提示网络状态
    [self showNetStatus];
    //
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        self.items = [[OrderModel getCourses] mutableCopy];
       dispatch_async(dispatch_get_main_queue(), ^{
           [self.tableView reloadData];
       });
    });
    // Do any additional setup after loading the view.
    //界面刷新
    UIRefreshControl* rc = [[UIRefreshControl alloc] init];
    [rc addTarget:self action:@selector(refreshAction) forControlEvents:UIControlEventValueChanged];
    rc.attributedTitle = [[NSAttributedString alloc] initWithString:@"下拉刷新"];
    self.refreshControl = rc;
    self.refreshControl.tintColor = [UIColor colorWithRed:16/255.0 green:101/255.0 blue:228/255.0 alpha:1.0];
}

//界面刷新

- (void)refreshAction{
    NSLog(@"开始刷新！");
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        self.items = [[OrderModel getCourses] mutableCopy];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.refreshControl endRefreshing];
            [self.tableView reloadData];
        });
    });
}

//刷新结束


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"我的收藏数据列表数据%d",self.items.count);
    return self.items.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString* CellIdentifier = [NSString stringWithFormat:@"Cell%ld%ld",(long)[indexPath section],(long)[indexPath row]];
    MyOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell)
    {
        cell = [[MyOrderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.course = self.items[indexPath.row];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    if ([[self.items[indexPath.row] orderStatus] integerValue] == 0) {
//        [self gotoAliPay:indexPath.row];
//    }else{
//        ShowOrderVC* showVC = [[ShowOrderVC alloc]initWithNibName:@"ShowOrderVC" bundle:nil];
//        showVC.course = self.items[indexPath.row];
//        [self.navigationController pushViewController:showVC animated:NO];
//    }
}


-(void)gotoAliPay:(long int)indexPathRow{
    SubmitOrder* order = [[SubmitOrder alloc] init];
    order.sourceId = @"2";
    order.deviceNo = @"IOS12345";
    NSDictionary* dicInfo = [[NSBundle mainBundle] infoDictionary] ;
    order.version = [dicInfo objectForKey:@"CFBundleShortVersionString"];
    order.agencyId = @"YX";
    order.userId = [[[User currentUser]userId] copy];
    order.classId = [[self.items[indexPathRow]classId] copy];
    order.courseId = [[self.items[indexPathRow] courseId] copy];
    order.teacherId = [[self.items[indexPathRow] teacherId] copy];
    order.orderAmount = @"0";
    order.realAmount = [[self.items[indexPathRow] realAmount] copy];
    order.discount = @"0";
    order.mobile = [[User currentUser]mobile];
    order.userName = @"益学用户";
    order.salesCode = @"益学顾问";
    order.invoiceType = @"";
    order.invoiceTitle = @"";
    order.invoiceAddress = @"";
    order.coupons = @"";
    order.showPhoneNum = [[NSUserDefaults standardUserDefaults]objectForKey:@"currentMobile"];
    SubmitOrderVc* subVC = [[SubmitOrderVc alloc] initWith:order Page:0];
    SubmitPay* pay = [[SubmitPay alloc]init];
    pay.orderId = [[self.items[indexPathRow] orderId] copy];
    pay.orderNo = [[self.items[indexPathRow] orderNo] copy];
    subVC.pay = pay;
    [self.navigationController pushViewController:subVC animated:NO];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
